package demoMod.nanaki.patches.events.city;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpireInsertPatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.events.city.Beggar;
import com.megacrit.cardcrawl.helpers.ScreenShake;
import com.megacrit.cardcrawl.localization.EventStrings;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.vfx.cardManip.PurgeCardEffect;
import com.megacrit.cardcrawl.vfx.combat.FlashAtkImgEffect;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.characters.NanakiCharacter;

public class BeggarPatch {
    public static final String ID = NanakiMod.makeID("Beggar");
    private static final EventStrings eventStrings;
    public static final String NAME;
    public static final String[] DESCRIPTIONS;
    public static final String[] OPTIONS;

    @SpirePatch(
            clz = Beggar.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        public static void Postfix(Beggar event) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                event.imageEventText.setDialogOption(OPTIONS[7]);
            }
        }
    }

    @SpirePatch(
            clz = Beggar.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        public static int lastChoice = 0;

        public static SpireReturn<Void> Prefix(Beggar event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                Beggar.CurScreen screen = ReflectionHacks.getPrivate(event, Beggar.class, "screen");
                switch (screen) {
                    case INTRO:
                        if (buttonPressed == 2) {
                            event.imageEventText.loadImage("images/events/cleric.jpg");
                            event.imageEventText.updateBodyText(DESCRIPTIONS[4]);
                            AbstractDungeon.player.damage(new DamageInfo(AbstractDungeon.player, 10));
                            CardCrawlGame.screenShake.shake(ScreenShake.ShakeIntensity.HIGH, ScreenShake.ShakeDur.LONG, false);
                            AbstractDungeon.effectList.add(new FlashAtkImgEffect(AbstractDungeon.player.hb.cX, AbstractDungeon.player.hb.cY, AbstractGameAction.AttackEffect.BLUNT_HEAVY));
                            event.imageEventText.clearAllDialogs();
                            event.imageEventText.setDialogOption(OPTIONS[4]);
                            ReflectionHacks.setPrivate(event, Beggar.class, "screen", Beggar.CurScreen.GAVE_MONEY);
                            lastChoice = 2;
                            return SpireReturn.Return(null);
                        } else {
                            lastChoice = buttonPressed;
                        }
                        break;
                    case GAVE_MONEY:
                        if (lastChoice == 2) {
                            AbstractDungeon.gridSelectScreen.open(CardGroup.getGroupWithoutBottledCards(AbstractDungeon.player.masterDeck.getPurgeableCards()), 1, OPTIONS[6], false, false, false, true);
                            event.imageEventText.updateBodyText(DESCRIPTIONS[5]);
                            event.imageEventText.clearAllDialogs();
                            event.imageEventText.setDialogOption(OPTIONS[5]);
                            ReflectionHacks.setPrivate(event, Beggar.class, "screen", Beggar.CurScreen.LEAVE);
                            return SpireReturn.Return(null);
                        }
                        break;
                }
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(
            clz = Beggar.class,
            method = "update"
    )
    public static class PatchUpdate {
        @SpireInsertPatch(rloc = 1)
        public static SpireReturn<Void> Insert(Beggar event) {
            if (AbstractDungeon.player instanceof NanakiCharacter && PatchButtonEffect.lastChoice == 2) {
                if (!AbstractDungeon.isScreenUp && !AbstractDungeon.gridSelectScreen.selectedCards.isEmpty()) {
                    CardCrawlGame.sound.play("CARD_EXHAUST");
                    AbstractEvent.logMetricCardRemovalAndDamage("Beggar", "Extradite", AbstractDungeon.gridSelectScreen.selectedCards.get(0), 10);
                    AbstractDungeon.topLevelEffects.add(new PurgeCardEffect(AbstractDungeon.gridSelectScreen.selectedCards.get(0), (float)(Settings.WIDTH / 2), (float)(Settings.HEIGHT / 2)));
                    AbstractDungeon.player.masterDeck.removeCard(AbstractDungeon.gridSelectScreen.selectedCards.get(0));
                    AbstractDungeon.gridSelectScreen.selectedCards.clear();
                    AbstractDungeon.getCurrRoom().phase = AbstractRoom.RoomPhase.COMPLETE;
                    AbstractDungeon.dungeonMapScreen.open(false);
                }
                return SpireReturn.Return(null);
            }
            return SpireReturn.Continue();
        }
    }

    static {
        eventStrings = CardCrawlGame.languagePack.getEventString(ID);
        NAME = eventStrings.NAME;
        DESCRIPTIONS = eventStrings.DESCRIPTIONS;
        OPTIONS = eventStrings.OPTIONS;
    }
}
